package com.liusand.controller;

import com.liusand.common.Result;
import com.liusand.service.UserService;
import com.liusand.dto.LoginDto;
import com.liusand.dto.SendMsgDto;
import com.liusand.vo.UserVo;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author :liusha
 * packageName :com.liusand.controller
 * fileName :UserController
 * creationTime :2022/11/30 16:25
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("sendMsg")
    public Result sendMsg(@RequestBody SendMsgDto sendMsgDto){
        if(sendMsgDto == null){
            return Result.error("手机验证码短信发送失败");
        }
        String phone = sendMsgDto.getPhone();

        boolean b = userService.sendMsg(phone);

        if(!b){
            return Result.error("手机验证码短信发送失败");
        }
        return Result.success("手机验证码短信发送成功");
    }

    @PostMapping("login")
    public Result login(@RequestBody LoginDto loginDto){
        if(loginDto == null){
            return Result.error("登录失败");
        }
        String phone = loginDto.getPhone();
        String code = loginDto.getCode();
        UserVo userVo = userService.login(phone, code);

        if(userVo == null){
            return Result.error("登录失败");
        }
        return new Result(1,null,userVo);
    }

    @PostMapping("loginout")
    public Result loginOut(@RequestHeader("token") String token){
        if(token == null){
            return Result.error("退出失败");
        }
        boolean re = userService.loginOut(token);
        if (!re) {
            return Result.error("退出失败");
        }
        return Result.success();
    }
}
